home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Sound / RAPlay / StreamRA.rx < prev    next >
Text File  |  1999-04-28  |  7KB  |  262 lines

  1. /* StreamRA.rx by Sigbjørn (CISC) Skjæret
  2. ** $VER: StreamRA.rx v2.3 (28.04.99)
  3. **
  4. ** Tries to stream a RealAudio file via a file containing URL(s) (like a .ram file)...
  5. */
  6. options results
  7. options failat 21
  8. signal on halt
  9. signal on break_c
  10. parse arg args
  11.  
  12.  
  13. /*** Edit these options ***/
  14.  
  15. raprog        = 'C:RAPlay'        /* Which decoder to run */
  16. rxprog        = 'SYS:RexxC/RX'    /* The ARexx interpreter */
  17.  
  18. bfsize        = '8192'            /* Initial playback buffer (will revert to "pbsize" variable after first read) */
  19. pbsize        = '1024'            /* Playback buffer (keep this low unless you have a very fast connection to the stream) */
  20.  
  21. savepath    = ''                /* Leave this empty unless you wish to save file in this dir while streaming */
  22. comment        = '1'                /* Save URL as comment to the file saved? (1 for yes, 0 for no) */
  23.  
  24. reqtools    = '1'                /* Display ReqTools requester? (1 for yes, 0 for no) */
  25. pubscreen    = 'Workbench'        /* The public screen you want the requester on */
  26.  
  27. /*** <-end-> ***/
  28.  
  29.  
  30. if ~show('L','rxsocket.library') then
  31. do
  32.  if ~addlib('rxsocket.library',0,-30,0) then
  33.  do
  34.   say 'ARexx socket library not available, exiting'
  35.   exit 10
  36.  end
  37. end
  38.  
  39. if(reqtools) then
  40.  if ~show('L','rexxreqtools.library') then
  41.   if ~addlib('rexxreqtools.library',0,-30,0) then reqtools=0
  42.  
  43. if(~exists(raprog) | ~exists(rxprog)) then
  44. do
  45.  if ~exists(rxprog) then notfound=rxprog
  46.  else notfound=raprog
  47.  
  48.  say 'Sorry, "'notfound'" does not exist, please edit path in script.'
  49.  exit 10
  50. end
  51.  
  52. if(upper(word(args,1))='URL') then
  53. do
  54.  url=word(args,2)
  55.  ratmp=word(args,3)
  56.  call Stream()
  57.  
  58.  call close(pipe)
  59.  call CloseSocket(sock)
  60.  if(save) then
  61.  do
  62.   call close(file)
  63.   if(comment) then address command 'FileNote "'savepath''filename'" http://'url
  64.  end
  65.  
  66.  exit
  67. end
  68. else if(upper(word(args,1))='BREAK') then
  69. do
  70.  pidtmp='pid'time('S')
  71.  address command 'Status >T:'pidtmp
  72.  call open(pid,'T:'pidtmp,R)
  73.  do while ~eof(pid)
  74.   line=readln(pid)
  75.   if(pos(raprog,line)>0) then
  76.   do
  77.    id=strip(word(line,2),'T',':');cmd=subword(line,6)
  78.    address command 'Break >NIL: 'id
  79.    say 'Sent Ctrl-C to process 'id': 'cmd
  80.   end
  81.  end
  82.  call close(pid)
  83.  address command 'Delete >NIL: T:'pidtmp' QUIET'
  84.  say 'Done.'
  85.  exit
  86. end
  87.  
  88. args=strip(args,'B','"')
  89.  
  90. parse source . . . pname;pname=subword(pname,1,words(pname)-2);found=0
  91.  
  92. if(pos(' ',pname)>0) then
  93. do
  94.  say 'Sorry, due to restrictions in RX, you''ll have to place "'pname'" somewhere where the path does not resolve with a space in it.'
  95.  exit 10
  96. end
  97.  
  98. if(args='') then
  99. do
  100.  if(reqtools) then url=rtgetstring(,'Enter URL:','Enter URL')
  101.  else do;say 'Enter URL:';parse pull url;end
  102.  
  103.  if(url='') then
  104.  do
  105.   say 'No URL entered .. exiting!'
  106.   exit
  107.  end
  108.  
  109.  if(pos('://',url)=0) then url='http://'url
  110.  call Play()
  111. end
  112. else if ~open(file,args,R) then
  113. do
  114.  say 'Unable to open file containing URL!!!'
  115.  exit 10
  116. end
  117. else do x=1 while ~eof(file)
  118.  url=readln(file);url=strip(url,'T',d2c(13))
  119.  if(url='') then iterate x
  120.  
  121.  if(~abbrev(url,'http://',7) & ~abbrev(url,'pnm://',6)) then iterate x
  122.  
  123.  call Play()
  124. end
  125.  
  126. if ~found then
  127. do
  128.  say 'No supported URLs found!!!'
  129.  exit 10
  130. end
  131.  
  132. exit
  133.  
  134.  
  135. /*** PROCEDURES ***/
  136.  
  137. Play:
  138.  if(upper(right(url,4))='.RAM' | upper(right(url,4))='.RPM') then
  139.  do
  140.   if(pos('http://',url)=1) then url=substr(url,8);if(pos('pnm://',url)=1) then url=substr(url,7);port='80';crlf=d2c(13)''d2c(10);lookup=1
  141.   if(pos('/',url)>0) then do;host=substr(url,1,pos('/',url)-1);path=substr(url,pos('/',url));end
  142.   else do;host=url;path='/';end
  143.   if(pos(':',host)>0) then do;port=substr(host,pos(':',host)+1);host=substr(host,1,pos(':',host)-1);end
  144.  
  145.   if ~GetHost('HOST',host) then;if(datatype(compress(host,'.'),'N')) then do;host.HOSTADDRLIST.0=host;lookup=0;end;else do;say 'Unable to lookup host!!!';iterate x;end
  146.   sock=Socket('INET','STREAM','IP');if sock<0 then do;say 'Unable to create socket!!!';iterate x;end
  147.   conn.ADDRFAMILY='INET';conn.ADDRPORT=port;conn.ADDRADDR=host.HOSTADDRLIST.0
  148.   if Connect(sock,'CONN')<0 then do;if(lookup) then say 'Sorry, server is down, unable to stream data!';else say 'Host does not exist!!!';iterate x;end
  149.  
  150.   req='GET 'path' HTTP/1.0'crlf'Host: 'host''crlf''crlf
  151.   call Send(sock,req)
  152.  
  153.   len=1
  154.   do y=1 while len>0
  155.    len=recvline(sock,'BUF',1024)
  156.    if(buf=crlf) then
  157.    do z=1 while len>0
  158.     len=recvline(sock,'BUF',1024)
  159.     url=strip(strip(strip(buf,'T',d2c(10)),'T',d2c(13)),'B')
  160.     if(url='') then iterate z
  161.     if(~abbrev(url,'http://',7) & ~abbrev(url,'pnm://',6)) then iterate z
  162.     leave y
  163.    end
  164.   end
  165.   call CloseSocket(sock)
  166.  end
  167.  
  168.  if(url='') then return 0
  169.  
  170.  ratmp='ra'time('S');setclip(ratmp);found=1
  171.  address command 'Run >NIL: 'rxprog' 'pname' URL 'url' 'ratmp
  172.  
  173.  do i=1 to 15
  174.   address command 'Wait 5'
  175.   clip=getclip(ratmp)
  176.   if(clip='PLAY') then leave i
  177.   if(clip~='') then
  178.   do
  179.    say 'Failed: 'clip
  180.    setclip(ratmp)
  181.    return 0
  182.   end
  183.   if(i=15) then
  184.   do
  185.    say 'Operation timed out...'
  186.    setclip(ratmp,'DIE') /* Just in case */
  187.    return 0
  188.   end
  189.  end
  190.  
  191.  address command ''raprog' <PIPE:'ratmp' >AUDIO:B/16/F/8000/T/SIGNED/BUFFER/32768'
  192.  if(rc=20) then setclip(ratmp,'DIE')
  193. return 0
  194.  
  195. Stream:
  196.  filename='';size=''
  197.  if(pos('http://',url)=1) then url=substr(url,8);if(pos('pnm://',url)=1) then url=substr(url,7);port='80';save=0
  198.  host=substr(url,1,pos('/',url)-1);path=substr(url,pos('/',url))
  199.  if(pos(':',host)>0) then do;port=substr(host,pos(':',host)+1);host=substr(host,1,pos(':',host)-1);end
  200.  
  201.  if(savepath~='') then
  202.  do
  203.   if(right(savepath,1)~=':' & right(savepath,1)~='/') then savepath=savepath'/'
  204.   filename=substr(path,lastpos('/',path)+1);save=1
  205.  end
  206.  
  207.  crlf=d2c(13)''d2c(10);lookup=1
  208.  
  209.  if ~GetHost('HOST',host) then;if(datatype(compress(host,'.'),'N')) then do;host.HOSTADDRLIST.0=host;lookup=0;end;else do;setclip(ratmp,'Unable to lookup host!!!');return 10;end
  210.  sock=Socket('INET','STREAM','IP');if sock<0 then do;setclip(ratmp,'Unable to create socket!!!');return 10;end
  211.  conn.ADDRFAMILY='INET';conn.ADDRPORT=port;conn.ADDRADDR=host.HOSTADDRLIST.0
  212.  if Connect(sock,'CONN')<0 then do;if(lookup) then setclip(ratmp,'Sorry, server is down, unable to stream data!');else setclip(ratmp,'Host does not exist!!!');return 10;end
  213.  
  214.  req='GET 'path' HTTP/1.0'crlf'Host: 'host''crlf''crlf
  215.  call Send(sock,req)
  216.  
  217.  call open(pipe,'PIPE:'ratmp,W)
  218.  if(save) then call open(file,savepath''filename,W)
  219.  
  220.  len=1;start=0
  221.  do y=1 while len>0
  222.   if(start) then
  223.   do
  224.    len=recv(sock,'BUF',bfsize);bfsize=pbsize
  225.    if(len>0) then
  226.    do
  227.     call writech(pipe,buf)
  228.     if(save) then call writech(file,buf)
  229.    end
  230.    if(getclip(ratmp)='DIE') then do;setclip(ratmp);return 0;end
  231.   end
  232.   else
  233.   do
  234.    len=recvline(sock,'BUF',1024)
  235.    if(y=1 & word(buf,2)~='200') then do;setclip(ratmp,strip(subword(buf,3),'T',crlf));return 10;end
  236.    if(word(buf,1)='Content-Length:') then size=strip(word(buf,2),'T',crlf)
  237.    if(buf=crlf) then do;start=1;setclip(ratmp,'PLAY');call OpenReq();end
  238.   end
  239.  end
  240. return 0
  241.  
  242. OpenReq:
  243.  if(reqtools) then
  244.  do
  245.   if(size='') then size='Unknown'
  246.   else size=size' bytes'
  247.   if(save=0) then svfl='No'
  248.   else svfl=savepath''filename
  249.   if(pubscreen='') then reqtags="'rtez_flags=ezreqf_noreturnkey'"
  250.   else reqtags="'rt_pubscrname="pubscreen" rtez_flags=ezreqf_noreturnkey'"
  251.  
  252.   req="'Host: "host"'d2c(10)'File: "path"'d2c(10)'Save: "svfl"'d2c(10)'Size: "size"','_Close|_Break','StreamRA...',"reqtags
  253.  
  254.   address command 'Run >NIL: 'rxprog' "call rtezrequest('req');if(rtresult=0) then setclip('''ratmp''',''DIE'')"'
  255.  end
  256. return 0
  257.  
  258. Halt:
  259. Break_C:
  260.  setclip(ratmp,'DIE')
  261. exit
  262.